<template>
  <div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        @click="$router.push(`/article/${item.art_id}`)"
        v-for="(item,index) in list"
        :key="index"
        :title="item.title"
      ></van-cell>
    </van-list>
  </div>
</template>
<script>
import { getResult } from '@/api/search.js'
export default {
  props: {
    searchText: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1
    }
  },
  methods: {
    async  onLoad () {
      // 异步更新数据
      try {
        const { data: res } = await getResult({
          page: this.page,
          per_page: 20,
          q: this.searchText
        })
        console.log(res.data.results)
        this.list.push(...res.data.results)
        this.loading = false
      } catch (err) {
        this.$toast('加载数据失败')
      }
    }
  }
}
</script>
